package com.x.provider.customer.factory.dto.impl;

import com.x.core.utils.BeanUtil;
import com.x.provider.api.customer.enums.CustomerOptions;
import com.x.provider.api.customer.model.dto.customer.CustomerDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerListReqDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerResourceDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerResumePreferenceDTO;
import com.x.provider.customer.factory.dto.CustomerDTOBuilder;
import com.x.provider.customer.model.domain.customer.CustomerResource;
import com.x.provider.customer.model.domain.customer.CustomerResumePreference;
import com.x.provider.customer.model.query.customer.CustomerResumePreferenceQuery;
import com.x.provider.customer.service.customer.CustomerResourceService;
import com.x.provider.customer.service.customer.CustomerResumePreferenceService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: liushenyi
 * @date: 2022/07/25/16:04
 */
@Service("customerResumePreferenceBuilder")
public class CustomerResumePreferenceBuilderImpl implements CustomerDTOBuilder {

   private final CustomerResumePreferenceService customerResumePreferenceService;

   public CustomerResumePreferenceBuilderImpl(CustomerResumePreferenceService customerResumePreferenceService){
      this.customerResumePreferenceService = customerResumePreferenceService;
   }

   @Override
   public void build(CustomerListReqDTO listCustomerAO, Map<Long, CustomerDTO> dest) {
      if (!listCustomerAO.getCustomerOptions().contains(CustomerOptions.CUSTOMER_RESUME_PREFERENCE.name())){
         return;
      }
      Map<Long, CustomerResumePreference> customerResourceMap = customerResumePreferenceService.list(CustomerResumePreferenceQuery.builder()
              .customerIdList(listCustomerAO.getCustomerIds()).build()).stream().collect(Collectors.toMap(item -> item.getCustomerId(), item -> item));
      customerResourceMap.entrySet().stream().forEach(item -> {
         dest.get(item.getKey()).setCustomerResumePreference(BeanUtil.prepare(item.getValue(), CustomerResumePreferenceDTO.class));
      });
   }
}
